package com.gtapps.user.action;

import com.gtapps.user.entity.BaseUserUserEntity;
import com.gtapps.user.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.result.ServletRedirectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.text.DecimalFormat;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

@Controller
@Namespace("/")
@Scope("prototype")
public class DataQueryAction extends ActionSupport {

    private static Logger logger = LogManager.getLogger(DataQueryAction.class);

    private static final Double DOUBLE_ZERO = 0.0;
    private List<BaseUserUserEntity> userList;
    private Double sum;

    private BaseUserUserEntity user;
    @Autowired
    private UserService userService;

    public List<BaseUserUserEntity> getUserList() {
        return userList;
    }

    public void setUserList(List<BaseUserUserEntity> userList) {
        this.userList = userList;
    }

    @Action(value = "queryData",
            results = {
                    @Result(name = "success", location = "index.html", type = "redirect")
            })


    public String queryDia() throws Exception {
        if (user != null) {
            userService.saveUser(user);
            return SUCCESS;
        } else {
            return "error";
        }


    }

    @Action(value = "reloadData", results = {
            @Result(name = "success", location = "/html/success.jsp")
    })

    public String queryAll() throws Exception {
        // System.out.print("===2====");
        //  Logger.
        userList = userService.queryUserAll();
        Double add_double = 0.0;
        for (BaseUserUserEntity user : userList) {
            add_double = add_double + Double.valueOf(user.getPassword());
        }
        sum = add_double;

        return SUCCESS;
    }


    @Action("formatDouble")
    public String formatDouble(double s) {
        DecimalFormat fmt = new DecimalFormat("\u00A4##0.00");
        return fmt.format(s);

    }

    //字符串转成数字
    @Action("formatStringToDouble")
    public Double formatStringToDouble(String str) {
        if (str == null || str.isEmpty()) {
            return DOUBLE_ZERO;
        }

        Double s = Double.valueOf(str.toString());

        return s;

    }


    public BaseUserUserEntity getUser() {
        return user;
    }

    public void setUser(BaseUserUserEntity user) {
        this.user = user;
    }

    public Double getSum() {
        return sum;
    }


    public void setSum(Double sum) {
        this.sum = sum;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
